﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;
using DataAccessLayer;
using BusinessLogicLayer;

namespace Maintain_supplier_information
{
    public partial class AddItem : System.Web.UI.Page
    {
        StationeryInventoryEntities1 se;
        SupplierBizlogic sbz = new SupplierBizlogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            se = new StationeryInventoryEntities1();
            if (!IsPostBack)
            {
                List<Object> category = new List<object>();
                category = sbz.getcategories();
                ddl_Category.DataSource = category;
                ddl_Category.DataTextField = "CategoryName";
                ddl_Category.DataValueField = "CategoryID";
                ddl_Category.DataBind();

                List<Object> suplist = new List<object>();
                suplist = sbz.getsuppliers();
                ddl_Supplier1.DataSource = suplist;
                ddl_Supplier1.DataTextField = "SupplierName";
                ddl_Supplier1.DataValueField = "SupplierID";
                ddl_Supplier1.DataBind();
                ddl_Supplier2.DataSource = suplist.ToList();
                ddl_Supplier2.DataTextField = "SupplierName";
                ddl_Supplier2.DataValueField = "SupplierID";
                ddl_Supplier2.DataBind();
                ddl_supplier3.DataSource = suplist.ToList();
                ddl_supplier3.DataTextField = "SupplierName";
                ddl_supplier3.DataValueField = "SupplierID";
                ddl_supplier3.DataBind();

 
            }
           

        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {

            using (TransactionScope ts = new TransactionScope())
            {
                
                Stationery s = new Stationery();
                Supplier sup = new Supplier();
                s.ItemCode = txt_ItemCode.Text;
                s.CategoryID = Convert.ToInt32(ddl_Category.SelectedValue.ToString());
                s.ItemName = txt_ItemName.Text;
                s.ReorderLevel = Convert.ToInt32(txt_reorderlevel.Text);
                s.ReorderQty = Convert.ToInt32(txt_reorderqty.Text);
                s.UOM = txt_UOM.Text;
                s.AvailableQty = Convert.ToInt32(txt_availQty.Text);
                s.BinNo = txt_BinNo.Text;
                se.Stationeries.Add(s);
                se.SaveChanges();
                sup.SupplierID = ddl_Supplier1.SelectedValue.ToString();
                decimal[] price = new decimal[3];
                price[0] = Convert.ToDecimal(txt_price1.Text);
                price[1] = Convert.ToDecimal(txt_price2.Text);
                price[2] = Convert.ToDecimal(txt_price3.Text);

                string[] sup_id = new string[3];
                sup_id[0] = ddl_Supplier1.SelectedValue.ToString();
                sup_id[1] = ddl_Supplier2.SelectedValue.ToString();
                sup_id[2] = ddl_supplier3.SelectedValue.ToString();

                for (int i = 1; i < 4; i++)
                {
                    SupplierDetail sd = new SupplierDetail();
                    sd.ItemCode = s.ItemCode;
                    sd.SupplierID = sup_id[i - 1];
                    sd.Price = price[i-1];
                    sd.Rank = i;
                    se.SupplierDetails.Add(sd);
                    se.SaveChanges();
                }                
                
                //se.Suppliers.Add(sup);
                //se.SaveChanges();



                



                ts.Complete();
            }
            
            
           
            


        }

        protected void btn_cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/MaintainStationeryItems.aspx");
        }
    }
}